<template>
    <el-form ref="form" :model="form" label-width="80px">
        
        <el-form-item label="项目类型">
            <el-select v-model="form.projectType" placeholder="请选择项目类型">
            <el-option :label="n.projectType" :value="n.id" v-for="n in projectTypes">
             
            </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="项目编号">
            <el-input v-model="form.projectNo"></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
            <el-input v-model="form.projectName"></el-input>
        </el-form-item>
        <el-form-item label="开始周期">
            <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.date1"   
             format="YYYY/MM/DD"
             value-format="YYYY-MM-DD" style="width: 100%;">
            </el-date-picker>
            </el-col>
            <el-col class="line" :span="2">结束周期:</el-col>
            <el-col :span="11">
            <el-date-picker placeholder="选择时间" v-model="form.date2" 
             format="YYYY/MM/DD"
             value-format="YYYY-MM-DD"
             style="width: 100%;"></el-date-picker>
            </el-col>
        </el-form-item>
        <el-form-item label="负责人">
            <el-select v-model="form.projectLeader" placeholder="请选择项目负责人">
            <el-option :label="n.username" :value="n.id" v-for="n in userList">
            </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="监控人">
            <el-select v-model="form.projectMonitorId" placeholder="请选择项目监控人">
            <el-option :label="n.username" :value="n.id" v-for="n in userList">
            </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="审批人">
            <el-select v-model="form.projectApproverId" placeholder="请选择项目审批人">
            <el-option :label="n.username" :value="n.id" v-for="n in userList">
            </el-option>
            </el-select>
        </el-form-item>
       
        <el-form-item label="紧急程度">
            <el-radio-group v-model="form.urgency">
            <el-radio :label="0" >高</el-radio>
            <el-radio :label="1" >很高</el-radio>
            <el-radio :label="2" >非常高</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="优先级别">
            <el-radio-group v-model="form.priority">
            <el-radio label="0" >低</el-radio>
            <el-radio label="1" >中</el-radio>
            <el-radio label="2" >高</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="项目描述">
            <el-input type="textarea" v-model="form.projectDescription"></el-input>
        </el-form-item>
         <el-form-item label="合同编号">
            <el-select v-model="form.contract" placeholder="请选择项目所属合同">
            <el-option :label="n.contractNumber" :value="n.id"  v-for="n in contracts">
                 <!-- {{n.contractSigner}}   -->
             </el-option> 
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button @click="cancel">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import { ElMessage } from 'element-plus'
  export default {
    
    data() {
      return {
        form: {
            projectName:'',//项目名
            projectNo:'',//项目编号
            date1: '',//开始时间
            date2: '',//结束时间
            projectLeader:'',//项目负责人
            projectType:'',//项目类型
            urgency:'',//紧急程度
            priority:'',//优先级别
            contract:'',//合同
            projectApproverId:'',//审批人id
            projectMonitorId:'',//监控人id
            projectDescription:''
        },
        userList:[],
        projectTypes:[],
        contracts:[]
      }
    },
    methods: {
      onSubmit() {
        var time= this.getDifferTime(this.form.date1,this.form.date2)+1;
        this.axios({
            method:'post',
            url:'http://localhost:8080/project/addProject',
            data:{
                projectTypeId:this.form.projectType,
                projectNoId:this.form.projectNo,
                projectName:this.form.projectName,
                projectCycle:time,
                urgency:this.form.urgency,
                priority:this.form.priority,
                projectApproverId:this.form.projectApproverId,
                projectMonitorId:this.form.projectMonitorId,
                projectDescription:this.form.projectDescription,
                founderId:'4',//模拟创建人id
                contractId:this.form.contract,
                projectLeader:this.form.projectLeader,
                startTime:this.form.date1,
                endTime:this.form.date2
            }
        }).then((response)=>{
            console.log(response);
            var status=response.status;
            var code=response.data.code;
            var message=response.data.message;
            if(status===200){
                if(code==200){
                    ElMessage({
                        message: message,
                        type: 'success',
                    })
                }else{
                    ElMessage.error(message)
                }
            }
        })
      },//计算相差天数
      getDifferTime(startDate, endDate) {
       let startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
       let endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
       let dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
       return dates;
    }
      ,
      messageList(){
        this.axios({
            method:'post',
            url:'http://localhost:8080/project/messageList',
            

        }).then((response)=>{
            console.log(response);
            var status=response.status;
            var data=response.data;
            if(status===200){
                if(data.code==200){
                  this.projectTypes= data.data.oaProjectTypes;
                   this.userList=data.data.list;
                   this.contracts=data.data.oaContracts;

                }
            }
        })
      },
      cancel(){
        this.form.projectName='';//项目名
        this.form.projectNo='';//项目编号
        this.form.date1= '';//开始时间
        this.form.date2= '';//结束时间
        this.form.projectLeader='';//项目负责人
        this.form.projectType='';//项目类型
        this.form.urgency='';//紧急程度
        this.form.priority='';//优先级别
        this.form.contract='';//合同
        this.form.projectApproverId='';//审批人id
        this.form.projectMonitorId='';//监控人id
        this.form.projectDescription=''
      }
    },
    beforeMount(){
        this.messageList();
    }
  }
</script>
<style>
</style>